// 设置 STOMP 客户端
var stompClient = null;
// 设置 WebSocket 进入端点
var SOCKET_ENDPOINT = "/boot/semiautomatic/websocket";
// 设置订阅消息的请求前缀
var SUBSCRIBE_PREFIX = "/boot/semiautomatic/topic";
// 设置订阅消息的请求地址
var SUBSCRIBE = "/boot/semiautomatic/topic/semiworkscreen";
// 设置服务器端点，访问服务器中哪个接口
var SEND_ENDPOINT = "/app/semiautomatic/getsemiautomaticscreeninfo";


var PublicResultCraneLocation = 0;
var secondCraneLocation = 0;
var thirdCraneLocation = 0;
var fourthCraneLocation = 0;


var firstCraneLocationY = 0;
var secondCraneLocationY = 0;
var thirdCraneLocationY = 0;
var fourthCraneLocationY = 0;


/* 进行连接 */
function connect() {
    // 设置 SOCKET
    var socket = new SockJS(SOCKET_ENDPOINT);
    // 配置 STOMP 客户端
    stompClient = Stomp.over(socket);
    stompClient.debug = null;

    // STOMP 客户端连接
    stompClient.connect({}, function (frame) {
        CXL.success("连接成功");
        subscribeSocket();
        setInterval("sendMessageNoParameter()", 2000);
    });
}

/* 订阅信息 */
function subscribeSocket() {
    // 执行订阅消息

    stompClient.subscribe(SUBSCRIBE, function (responseBody) {
        var receiveMessage = JSON.parse(responseBody.body);

        switch (receiveMessage.infoType) {
            case "cranestatus":
                var cranestatusList = JSON.parse(receiveMessage.detail);
                let firsticon = document.getElementById("firstcranestatusdisplay");
                let secondticon = document.getElementById("secondcranestatusdisplay");
                let thirdicon = document.getElementById("thirdcranestatusdisplay");
                let fourthicon = document.getElementById("fourthcranestatusdisplay");
                if (cranestatusList["firstcrane"] === true) {
                    firsticon.style.color = "red";
                } else {
                    firsticon.style.color = "green";
                }
                if (cranestatusList["secondcrane"] === true) {
                    secondticon.style.color = "red";
                } else {
                    secondticon.style.color = "green";
                }
                if (cranestatusList["thirdcrane"] === true) {
                    thirdicon.style.color = "red";
                } else {
                    thirdicon.style.color = "green";
                }
                if (cranestatusList["fourthcrane"] === true) {
                    fourthicon.style.color = "red";
                } else {
                    fourthicon.style.color = "green";
                }


                // $("#firstcranestatusdisplay").attr("color", getstatuscolor(cranestatusList["firstcrane"]));
                // $("#secondcranestatusdisplay").attr("color", getstatuscolor(cranestatusList["secondcrane"]));
                // $("#thirdcranestatusdisplay").attr("color", getstatuscolor(cranestatusList["thirdcrane"]));
                // $("#fourthcranestatusdisplay").attr("color", getstatuscolor(cranestatusList["fourthcrane"]));
                // $("#taskprogress").attr("lay-percent", (String(receiveMessage.detail)+"%"));
                break;
            case "cranelocationx":
                // console.log(receiveMessage.detail);

                var craneLocationList = JSON.parse(receiveMessage.detail);
                // console.log(craneLocationList);
                firstCraneLocation = parseInt(craneLocationList["firstcrane"]);
                secondCraneLocation = parseInt(craneLocationList["secondcrane"]);
                thirdCraneLocation = parseInt(craneLocationList["thirdcrane"]);
                fourthCraneLocation = parseInt(craneLocationList["fourthcrane"]);
                // console.log(firstCraneLocation);
                // updateCraneLocations();
                // $("#taskName").text(receiveMessage.detail);
                break;
            case "cranelocationy":
                // console.log(receiveMessage.detail);
                var craneLocationYList = JSON.parse(receiveMessage.detail);
                firstCraneLocationY = parseInt(craneLocationYList["firstcrane"]);
                secondCraneLocationY = parseInt(craneLocationYList["secondcrane"]);
                thirdCraneLocationY = parseInt(craneLocationYList["thirdcrane"]);
                fourthCraneLocationY = parseInt(craneLocationYList["fourthcrane"]);
                // $("#taskName").text(receiveMessage.detail);
                break;
            case "taskstatus":
                var taskStatusMap = JSON.parse(receiveMessage.detail);
                // document.getElementById("cranePauseButton1").innerHTML=(getIsPause(taskStatusMap["firstcrane"]));
                // document.getElementById("cranePauseButton2").innerHTML=(getIsPause(taskStatusMap["secondcrane"]));
                // document.getElementById("cranePauseButton3").innerHTML=(getIsPause(taskStatusMap["thirdcrane"]));
                // document.getElementById("cranePauseButton4").innerHTML=(getIsPause(taskStatusMap["fourthcrane"]));
                break;
            case "faultstatus":
                let faultStatusMap = JSON.parse(receiveMessage.detail);
                for(let key in faultStatusMap){
                    // console.log(getCraneStr(currentSelectedCrane));
                    if(key.toString() === getCraneStr(currentSelectedCrane)){
                        // console.log(key);
                        logFault(faultStatusMap[key]);
                        break;
                    }
                }

                // document.getElementById("cranePauseButton1").innerHTML=(getIsPause(taskStatusMap["firstcrane"]));
                // document.getElementById("cranePauseButton2").innerHTML=(getIsPause(taskStatusMap["secondcrane"]));
                // document.getElementById("cranePauseButton3").innerHTML=(getIsPause(taskStatusMap["thirdcrane"]));
                // document.getElementById("cranePauseButton4").innerHTML=(getIsPause(taskStatusMap["fourthcrane"]));
                break;

            default:
                console.log("Error Messsage！");
        }
    });
}

/* 断开连接 */
function disconnect() {
    stompClient.disconnect(function () {
        alert("断开连接");
    });
}

/* 发送消息并指定目标地址（这里设置的目标地址为自身订阅消息的地址，当然也可以设置为其它地址） */
function sendMessageNoParameter() {
    var sendContent = "get";
    // 设置待发送的消息内容
    var message = '{"destination": "' + SUBSCRIBE + '", "content": "' + sendContent + '"}';
    // 发送消息
    // console.log(SEND_ENDPOINT);
    stompClient.send(SEND_ENDPOINT, {}, message);
}

//发送消息获取任务状态
function sendMessageForGetTastInfo(craneId) {
    // console.log(craneId);
    var message = getMessage(craneId);
    //发送消息
    stompClient.send(SEND_ENDPOINT, {}, message);
}

function getMessage(craneId) {
    var GET_TASK_INFO_PREFIX = "getTaskInfoCrane";
    var message;

    message = '{"destination": "' + SUBSCRIBE + '", "content": "' + GET_TASK_INFO_PREFIX + craneId.toString() + '"}';
    return message;

}

function getSubSCRIBE(craneId) {
    SUBSCRIBE = SUBSCRIBE + craneId;
}




//获取状态颜色
function getstatuscolor(statusboolean) {
    if (statusboolean === true) {
        return heightColorArr[9];
    } else {
        return heightColorArr[0];
    }
}

function getIsPause(statusboolean) {
    if (statusboolean === false) {
        return "暂停";
    } else {
        return "继续";
    }
}

function getCraneStr(craneId) {
    switch(craneId) {
        case 1:
            return "firstcrane";
            break;
        case 2:
            return "secondcrane";
            break;
        case 3:
            return "thirdcrane";
            break;
        case 4:
            return "fourthcrane";
            break;
        default:
            return null;
    }
}